/*
  Copyright (c) 2012, Qusai Al Abdallah and Omar Fawzi 
 * All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, 
  are permitted provided that the following conditions are met:

    - Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
       
    - Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
       

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/


package primerdb;

import java.math.*;
import javax.swing.*;

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

import javax.swing.filechooser.*;

public class PrimerCalculator extends javax.swing.JFrame {

    int x=1;
    JFileChooser fc;
 
    
    /** Creates new form PrimerCalculator */
    public PrimerCalculator() {
        //super(parent, modal);
        initComponents();
        setResizable(false);
        
        primerSequence.addMouseListener(new ContextMenuMouseListener());
        primerName.addMouseListener(new ContextMenuMouseListener());
        outcome.addMouseListener(new ContextMenuMouseListener());
        
        fc = new JFileChooser();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        errDialog = new javax.swing.JDialog();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        primerLength = new javax.swing.JCheckBox();
        ATCGComposition = new javax.swing.JCheckBox();
        GCContent = new javax.swing.JCheckBox();
        primerSequenceCB = new javax.swing.JCheckBox();
        method1 = new javax.swing.JCheckBox();
        method2 = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();
        meltingTemp = new javax.swing.JCheckBox();
        annealingTemp = new javax.swing.JCheckBox();
        lblNaConc1 = new javax.swing.JLabel();
        lblNaConc = new javax.swing.JLabel();
        txtNaConc = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        outcome = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        btnExportText = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        primerSequence = new javax.swing.JTextArea();
        primerName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        javax.swing.GroupLayout errDialogLayout = new javax.swing.GroupLayout(errDialog.getContentPane());
        errDialog.getContentPane().setLayout(errDialogLayout);
        errDialogLayout.setHorizontalGroup(
            errDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        errDialogLayout.setVerticalGroup(
            errDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Primer Analyzer");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(289, 27));

        jLabel3.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
        jLabel3.setText("Step 1: Enter Primer Name and Sequence");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(73, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        primerLength.setBackground(new java.awt.Color(223, 223, 223));
        primerLength.setFont(new java.awt.Font("Arial", 1, 12));
        primerLength.setText("Primer Length");
        primerLength.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                primerLengthActionPerformed(evt);
            }
        });

        ATCGComposition.setBackground(new java.awt.Color(223, 223, 223));
        ATCGComposition.setFont(new java.awt.Font("Arial", 1, 12));
        ATCGComposition.setText("A, T, C, G Composition of Primer");

        GCContent.setBackground(new java.awt.Color(223, 223, 223));
        GCContent.setFont(new java.awt.Font("Arial", 1, 12));
        GCContent.setText("GC Content (%)");

        primerSequenceCB.setBackground(new java.awt.Color(223, 223, 223));
        primerSequenceCB.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        primerSequenceCB.setText("Primer Sequence");

        method1.setBackground(new java.awt.Color(223, 223, 223));
        method1.setFont(new java.awt.Font("Arial", 1, 11));
        method1.setText("Basic Tm (recommended)");
        method1.setEnabled(false);
        method1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                method1ActionPerformed(evt);
            }
        });

        method2.setBackground(new java.awt.Color(223, 223, 223));
        method2.setFont(new java.awt.Font("Arial", 1, 11));
        method2.setText("Salt Adjusted Tm");
        method2.setEnabled(false);
        method2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                method2ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel7.setText("Temperature (Tm) calculation method");

        meltingTemp.setBackground(new java.awt.Color(223, 223, 223));
        meltingTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        meltingTemp.setText("Melting Temperature (Tm)");
        meltingTemp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                meltingTempActionPerformed(evt);
            }
        });

        annealingTemp.setBackground(new java.awt.Color(223, 223, 223));
        annealingTemp.setFont(new java.awt.Font("Arial", 1, 12));
        annealingTemp.setText("Annealing Temperature (Ta = Tm - 5)");
        annealingTemp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annealingTempActionPerformed(evt);
            }
        });

        lblNaConc1.setBackground(new java.awt.Color(223, 223, 223));
        lblNaConc1.setText("mM");
        lblNaConc1.setEnabled(false);

        lblNaConc.setBackground(new java.awt.Color(223, 223, 223));
        lblNaConc.setText("[Na+]");
        lblNaConc.setEnabled(false);

        txtNaConc.setText("50");
        txtNaConc.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(primerSequenceCB)
                    .addComponent(primerLength)
                    .addComponent(ATCGComposition)
                    .addComponent(GCContent, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(meltingTemp)
                    .addComponent(annealingTemp)
                    .addComponent(method1)
                    .addComponent(jLabel7)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(method2)
                        .addGap(31, 31, 31)
                        .addComponent(lblNaConc)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNaConc, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNaConc1)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(primerSequenceCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(primerLength)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(GCContent)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ATCGComposition)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(meltingTemp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(annealingTemp)
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(method1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(method2)
                    .addComponent(lblNaConc)
                    .addComponent(txtNaConc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNaConc1))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel4.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
        jLabel4.setText("Step 2: Check Boxes to be Analyzed");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(67, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel5.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
        jLabel5.setText("Step 3: Once Ready, Hit \"Analyze\"           ==>");

        jButton1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jButton1.setText("Analyze");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(233, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        outcome.setColumns(20);
        outcome.setLineWrap(true);
        outcome.setRows(5);
        outcome.setPreferredSize(null);
        jScrollPane2.setViewportView(outcome);

        jLabel6.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel6.setText("Step 4: Export the Analysis Report (Below) into");

        btnExportText.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btnExportText.setText("Text file");
        btnExportText.setEnabled(false);
        btnExportText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportTextActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 655, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(51, 51, 51)
                        .addComponent(btnExportText, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnExportText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        primerSequence.setColumns(20);
        primerSequence.setLineWrap(true);
        primerSequence.setRows(5);
        primerSequence.setName("primer-sequence"); // NOI18N
        primerSequence.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                primerSequenceMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(primerSequence);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("Primer Sequence");

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setText("Primer Name");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(primerName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(primerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
  public static double round(double d, int decimalPlace){
    // see the Javadoc about why we use a String in the constructor
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
  }
  
  String s = "";
  String strPrimerName = "";
  
  private void compute() {   
     
        s = "";
        strPrimerName = primerName.getText();
        String strPrimerNameNoSpaces = strPrimerName.replaceAll(" ", "");
        if(!strPrimerNameNoSpaces.isEmpty()) {
          s = s + "Primer name: " + strPrimerName + "\n";
        }
        String strPrimerSequence =  primerSequence.getText().toUpperCase();
        strPrimerSequence = strPrimerSequence.replaceAll(" ", "");
        strPrimerSequence = strPrimerSequence.replaceAll("\n", "");
        
        int intPrimerLength = strPrimerSequence.length();
        
        char[] strPrimerSequenceNew = new char[intPrimerLength];
        
        int counter = 0;      
        boolean errMessage = false;
        for(int i=0 ; i<intPrimerLength ; i++) {
            if(strPrimerSequence.charAt(i) != 'A' && strPrimerSequence.charAt(i) != 'T' &&
                    strPrimerSequence.charAt(i) != 'C' && strPrimerSequence.charAt(i) != 'G') {
               if(strPrimerSequence.charAt(i) != ' ' && strPrimerSequence.charAt(i) != '\n') {
                  errMessage = true;
               }
            }
            else {
                strPrimerSequenceNew[counter] = strPrimerSequence.charAt(i);
                counter++;
            }
        }
        if(errMessage) JOptionPane.showMessageDialog(errDialog,"Your sequence contains invalid characters. Invalid characters are omitted in the analysis.", "Warning", JOptionPane.WARNING_MESSAGE);

        intPrimerLength = counter;
        strPrimerSequence = String.copyValueOf(strPrimerSequenceNew, 0, counter);


       
        String z = "";        
               
        counter = 0;
        int mod3 = 0;
        while(counter < intPrimerLength) {
            if(mod3 == 3) {
                z += " ";
                mod3 = 0;
            }
            else {
                z += strPrimerSequence.charAt(counter);                
                counter++;
                mod3++;
            }
        }
        
        if(primerSequenceCB.isSelected() && !z.isEmpty()) {
          s = s + "Primer sequence: 5'-- " + z + " --3'\n";
        }
        
        
        // Primer length
        if(primerLength.isSelected()) {
          s = s + "Primer length: " + intPrimerLength + "\n";
        }
                
        // Counting ACGT
        int na = 0, nc = 0, ng = 0, nt = 0;
        
        for(int i=0; i < intPrimerLength ; i++) {
            if(strPrimerSequence.charAt(i) == 'A') na++;
            if(strPrimerSequence.charAt(i) == 'C') nc++;
            if(strPrimerSequence.charAt(i) == 'G') ng++;
            if(strPrimerSequence.charAt(i) == 'T') nt++;            
        }
        int gc = 0;
        try {
        gc = 10000 * (nc + ng)/ intPrimerLength;
        } catch(Exception e) {
        }
        
        double dblGC = (double) gc/ 100.0;
        
        if(GCContent.isSelected()) {
        s = s + "GC %: " + dblGC + "%\n";
        }
        
        if(ATCGComposition.isSelected()) {
          s = s + "The number of A: " + na + "\n";
          s = s + "The number of T: " + nt + "\n";
          s = s + "The number of C: " + nc + "\n";
          s = s + "The number of G: " + ng + "\n";
        }
        
        
        // Melting temperature computation
        
        double T1 = 0.0, T1b = 0.0, T2 = 0.0, T3 = 0.0;
        
        // Method 1 (Basic melting temperature, see also german company mwg)           
        
        if(intPrimerLength < 15) {
            T1 = 2*(na + nt) + 4*(nc+ng);
            T1b = 2*(na + nt) + 4*(nc+ng);
        }
        else {
        try {   
          T1 = round(69.3 + (double) 41*(nc + ng)/ ((double) intPrimerLength) - 650.0 / intPrimerLength, 1);
          T1b = round(64.9 + (double) 41*(nc + ng - 16.4)/ ((double) intPrimerLength), 1);
        } catch(Exception e) {
           
        }
        }
        
        
        
        
        // Method 2
        
        int naConc = 1;
        if(method2.isSelected()) {
        try {
          naConc = Integer.parseInt(txtNaConc.getText());
        }
        catch(Exception e) {
          JOptionPane.showMessageDialog(errDialog,"You did not enter a valid [Na+] concentration.\n"
                  + "The default 50mM concentration will be used.", "Warning", JOptionPane.WARNING_MESSAGE);
          naConc = 50;
        }
        if(naConc == 0) {
          JOptionPane.showMessageDialog(errDialog,"You did not enter a valid [Na+] concentration.\n"
                  + "The default 50mM concentration will be used.", "Warning", JOptionPane.WARNING_MESSAGE);
          naConc = 50;
        }
        
               
        if(intPrimerLength < 14) {
           T2 = round(2.0*( (double) (na+nt) ) + 4.0*( (double) (nc+ng)) - 16.6 * Math.log10(0.050) + 16.6*Math.log10(naConc/1000.0),1); 
        }
        else {
           try {
           T2 = round(100.5 + (double) (41.0 * (ng+nc)) / (double) (intPrimerLength) - 820.0/((double) intPrimerLength) 
                   + 16.6* Math.log10(naConc/1000.0),1);
           }
           catch(Exception e) {
              
           }
        } 
        }

        if((meltingTemp.isSelected() || annealingTemp.isSelected()) && !method1.isSelected() && !method2.isSelected()) {
          JOptionPane.showMessageDialog(errDialog,"In order to calculate the melting and annealing temperature, you have to choose a calculation method."
                  , "Warning", JOptionPane.WARNING_MESSAGE);          
        }
        
        if(meltingTemp.isSelected() && (method1.isSelected() || method2.isSelected())) {
        s += "Melting Temperature:\n";
        if(method1.isSelected()) {
        s += "       " + round(T1,1) + "°C (Basic method)\n";
        }
        if(method2.isSelected()) {
        s += "       " + round(T2,1) + "°C (Salt Adjusted method)\n";
        }
        }
        
        // Annealing temperature
        
        double annealingT1 = T1 - 5;
        double annealingT2 = T2 - 5;

        
        if(annealingTemp.isSelected() && (method1.isSelected() || method2.isSelected())) {
          s += "Annealing Temperature:\n";
          if(method1.isSelected()) {
            s += "       " +  round(annealingT1,1) + "°C (Basic method)\n";
          }
          if(method2.isSelected()) {
            s += "       " +  round(annealingT2,1) + "°C (Salt Adjusted method)\n";
          }

        }
    }
  
    private void printOutcome() {
        outcome.setText(s); 
    }
  
    private void exportText() {
       if(!s.isEmpty()){
       try {
       //File file = new File("primers/Primer_"+ strPrimerName + ".txt");
       File file = new File(strPrimerName + ".txt");
       fc.setSelectedFile(file); 
       int returnVal = fc.showSaveDialog(PrimerCalculator.this);
       if (returnVal == JFileChooser.APPROVE_OPTION) {
          file = fc.getSelectedFile();
          Writer output = null;
          output = new BufferedWriter(new FileWriter(file));
          output.write(s);
          output.close();
       }
       else {
          System.out.println("Error when writing text file " + returnVal);
       }
       } catch(Exception e) {
          System.out.println("Error when writing text file " + e.toString());
       }
       }
    }
  
  
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        compute();
        printOutcome();
        btnExportText.setEnabled(true);
        //jTextArea2.setText(jTextArea1.getText() + " is very good");
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    
    private void method1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_method1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_method1ActionPerformed

private void method2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_method2ActionPerformed
   if(method2.isSelected()) {
      lblNaConc.setEnabled(true);
      lblNaConc1.setEnabled(true);
      txtNaConc.setEnabled(true);
   }
   else {
      lblNaConc.setEnabled(false);
      lblNaConc1.setEnabled(false);
      txtNaConc.setEnabled(false);      
   }
}//GEN-LAST:event_method2ActionPerformed

private void meltingTempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_meltingTempActionPerformed
   if(meltingTemp.isSelected()) {
      method1.setEnabled(true);
      method2.setEnabled(true);
   }
   else if(!meltingTemp.isSelected() && !annealingTemp.isSelected()) {
      method1.setEnabled(false);
      method2.setEnabled(false);    
      lblNaConc.setEnabled(false);
      lblNaConc1.setEnabled(false);
      txtNaConc.setEnabled(false);       
   }
}//GEN-LAST:event_meltingTempActionPerformed

private void annealingTempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annealingTempActionPerformed
   if(annealingTemp.isSelected()) {
      method1.setEnabled(true);
      method2.setEnabled(true);
   }
   else if(!meltingTemp.isSelected() && !annealingTemp.isSelected()) {
      method1.setEnabled(false);
      method2.setEnabled(false);   
      lblNaConc.setEnabled(false);
      lblNaConc1.setEnabled(false);
      txtNaConc.setEnabled(false);       
   }
}//GEN-LAST:event_annealingTempActionPerformed

    private void primerLengthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_primerLengthActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_primerLengthActionPerformed

private void primerSequenceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_primerSequenceMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_primerSequenceMouseClicked

private void btnExportTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportTextActionPerformed
   exportText();
}//GEN-LAST:event_btnExportTextActionPerformed
    /**
     * @param args the command line arguments
     */
   
    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PrimerCalculator dialog = new PrimerCalculator();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
                
                // Add listener for right click
                
                
            }
        });
    }
    /**
     * @param args the command line arguments
     */
   
   private javax.swing.JWindow errWindow; 
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ATCGComposition;
    private javax.swing.JCheckBox GCContent;
    private javax.swing.JCheckBox annealingTemp;
    private javax.swing.JButton btnExportText;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JDialog errDialog;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblNaConc;
    private javax.swing.JLabel lblNaConc1;
    private javax.swing.JCheckBox meltingTemp;
    private javax.swing.JCheckBox method1;
    private javax.swing.JCheckBox method2;
    private javax.swing.JTextArea outcome;
    private javax.swing.JCheckBox primerLength;
    private javax.swing.JTextField primerName;
    private javax.swing.JTextArea primerSequence;
    private javax.swing.JCheckBox primerSequenceCB;
    private javax.swing.JTextField txtNaConc;
    // End of variables declaration//GEN-END:variables
}
